package com.osgi.desktop.boot;

import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.onetwork.core.boot.Main;
import com.onetwork.core.resource.Resource;
import com.onetwork.core.resource.ResourceType;
import com.osgi.desktop.ui.OSGIFrame;
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import org.knopflerfish.framework.Framework;
import org.osgi.framework.Constants;

public class Boot extends Main {
    
    private ObjectContainer objectContainer;
    private Framework framework;
    
    @Override
    public void configure() {
        try {
            String userDir = System.getProperty("user.home")+"/.softagregator/";
            File userDirFile = new File(userDir);
            if (!userDirFile.exists()) userDirFile.mkdir();
            Framework.setProperty(Constants.FRAMEWORK_SYSTEMPACKAGES, "javax.accessibility,javax.net,javax.net.ssl,javax.swing,javax.swing.border,javax.swing.event,javax.swing.filechooser,javax.swing.plaf,javax.swing.plaf.basic,javax.swing.plaf.metal,javax.swing.table,javax.swing.text,javax.swing.tree,com.osgi.desktop.ui,com.osgi.desktop.services,com.onetwork.core.actions,com.osgi.desktop.bundle,com.onetwork.core.ui.components,imagens,com.onetwork.core.domain,org.jdesktop.swingx,javax.imageio,javax.management,javax.sql,javax.naming,org.ietf.jgss,javax.crypto.spec,javax.crypto,javax.xml.parsers,org.w3c.dom,javax.swing.plaf,org.jdesktop.swingx.painter,org.jdesktop.jxlayer.plaf.ext,com.sun.scenario.scenegraph,org.jdesktop.jxlayer.plaf,org.jdesktop.jxlayer,org.jdesktop.jxlayer.plaf.effect,com.sun.scenario.effect,com.sun.scenario.scenegraph.fx");
            Framework.setProperty(Constants.EXPORT_PACKAGE, "com.osgi.desktop.ui,com.osgi.desktop.services");
            Framework.setProperty("org.osgi.framework.dir", userDir+"fwdir");
            Framework.setProperty("org.knopflerfish.gosg.jars", "jars");
            Framework.setProperty("org.knopflerfish.verbosity", "0");
            this.framework = new Framework(new Object());
            Resource.add(ResourceType.OSGIFramework, this.framework);
            this.objectContainer = Db4o.openFile(userDir+"softagregator.dat");
            Resource.add(ResourceType.Persistence, this.objectContainer);
        } catch (Exception ex) {
            Logger.getLogger(OSGIFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        Thread hook = new Thread(new Runnable() {
            @Override
            public void run() { objectContainer.close(); }
        });
        Runtime.getRuntime().addShutdownHook(hook);
        onetworkFrame = new OSGIFrame();
    }
    
    public static void main(String args[]) {
        try { 
            UIManager.setLookAndFeel(new NimbusLookAndFeel()); 
            new Boot();
        } catch (Exception ex) { 
            Logger.getLogger(OSGIFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
